
//
//  ViewController.m
//  UI05_UIControl子类
//
//  Created by Scott on 16/3/15.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "ImageViewOfAnimation.h"
#import "MySwitch.h"
#import "MySlider.h"
#import "MySegment.h"
#import "MyPageControl.h"
#import "Mybutton.h"

@interface ViewController ()

@property (nonatomic, retain) ImageViewOfAnimation *imageView;

@property (nonatomic, retain) MySwitch *switchOfAnimation;

@property (nonatomic, retain) MySlider *slider;

@property (nonatomic, assign) BOOL isBlack;


@end

@implementation ViewController


- (void)dealloc {
    
    [_imageView release];
    [_switchOfAnimation release];
    [_slider release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self createImageView];
    [self createSwitch];
    [self createSlider];
    [self createSegment];
    [self createPageControl];
    [self createButton];
    
}

- (void)createImageView {
    
    self.imageView = [[ImageViewOfAnimation alloc] initWithImage:[UIImage imageNamed:@"angry_00.jpg"]];
    
    self.imageView.frame = CGRectMake(0, 0, 300, 400);
    
    self.imageView.center = self.view.center;
    
    [self.view addSubview:self.imageView];
    [_imageView release];
    
}

#pragma mark - 知识点1: UISwitch
- (void)createSwitch {
    
    self.switchOfAnimation = [[MySwitch alloc] initWithFrame:CGRectMake(50, 550, 0, 0)];
    [self.view addSubview:self.switchOfAnimation];
    [_switchOfAnimation release];
    
    
    // 添加事件. 注意:控制事件是 值变化.
    [self.switchOfAnimation addTarget:self action:@selector(handleSwitch:) forControlEvents:UIControlEventValueChanged];
}
- (void)handleSwitch:(MySwitch *)swi {
    
    NSMutableArray *mArr = [NSMutableArray array];
    
    if (swi.on) {
        //开始动画.
        
        for (int i = 0; i < 26; i++) {
            
            NSString *name = [NSString stringWithFormat:@"angry_%02d", i];
            
            // 创建文件路径.
            NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"jpg"];
            
            
            // 内存比imageNamed:方法优化
            UIImage *image = [UIImage imageWithContentsOfFile:path];
            
            [mArr addObject:image];
        }
        
        self.imageView.animationImages = mArr;
        
        self.imageView.animationRepeatCount = 0;
        self.imageView.animationDuration = 10 - self.slider.value;
        
        [self.imageView startAnimating];

    } else {
        
        [self.imageView stopAnimating];
        
        self.imageView.animationImages = nil;
        
        [mArr removeAllObjects];
    }
}

#pragma mark - 知识点2: UISlider
- (void)createSlider {
    
    self.slider = [[MySlider alloc] initWithFrame:CGRectMake(80, 600, 200, 40)];
    [self.view addSubview:self.slider];
    [_slider release];
    
    [self.slider addTarget:self action:@selector(handleSlider:) forControlEvents:UIControlEventValueChanged];
    
    // 最小, 最大值赋值.
    self.slider.minimumValue = 1;
    self.slider.maximumValue = 10;
    
}
- (void)handleSlider:(MySlider *)slider {
    
    // 当动画执行时.
    if (self.switchOfAnimation.on) {
        
        self.imageView.animationDuration = 10 - slider.value ;
        
        [self.imageView startAnimating];
    }
}

#pragma mark - 知识点3: UISegmentedControl
- (void)createSegment {
    
    // 数组参数中放入字符串对象或image对象.
    MySegment *segment = [[MySegment alloc] initWithItems:@[@"白色", @"黄色", @"蓝色"]];
    segment.frame = CGRectMake(50, 50, 300, 40);
    
    [self.view addSubview:segment];
    [segment release];
    
    [segment addTarget:self action:@selector(handleSegment:) forControlEvents:UIControlEventValueChanged];
}

- (void)handleSegment:(MySegment *)segment {
    
    switch (segment.selectedSegmentIndex) {
        case 0:
            self.view.backgroundColor = [UIColor whiteColor];
            break;
        case 1:
            self.view.backgroundColor = [UIColor yellowColor];
            break;
        case 2:
            self.view.backgroundColor = [UIColor blueColor];
            break;
            
        default:
            break;
    }
    
    
}

#pragma mark - 知识点4: UIPageControl
- (void)createPageControl {
    
    MyPageControl *pageControl = [[MyPageControl alloc] initWithFrame:CGRectMake(20, 350, 100, 40)];
    [self.imageView addSubview:pageControl];
    [pageControl release];
    
    // 设置页数
    pageControl.numberOfPages = 5;
    
    [pageControl addTarget:self action:@selector(handlePage:) forControlEvents:UIControlEventValueChanged];
}
- (void)handlePage:(MyPageControl *)page {
    
    
}


#pragma mark - button  
- (void)createButton {
    
    Mybutton *button = [Mybutton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(120, 550, 60, 40);
    [self.view addSubview:button];
    
    [button setTitle:@"开启" forState:UIControlStateNormal];
    
    [button setTitle:@"关闭" forState:UIControlStateSelected];
    
    [button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handleButton:(Mybutton *)button {
    
    // UIControl有个selected属性(BOOL类型).
    button.selected = !button.selected;
    
    if (button.isSelected) {
        button.backgroundColor = [UIColor redColor];
    } else {
        button.backgroundColor = [UIColor greenColor];
    }
    
    NSLog(@"%d", button.selected);
    
    
    
    
    
}






#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
